<!DOCTYPE html>
<html>

	<head lang="en">
		<meta charset="UTF-8">
		<title>登录</title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<meta name="format-detection" content="telephone=no">
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-siteapp" />

		<link rel="stylesheet" href="static/css/amazeui.css" />
		<link href="static/css/dlstyle.css" rel="stylesheet" type="text/css">
		<link rel="stylesheet" href="static/css/bootstrap.min.css" />	
	</head>

	<body>

		<div class="login-boxtitle" style="height: 100px;">
			<div class="logoBig">
				<img src="static/images/logo.png" style="margin-left: 0px ; height: 100px;">
			</div>
		</div>

		<div class="login-banner" id="container">
			<div class="login-main" >
				<div class="login-banner-bg"><span></span><img src="static/images/big.jpg" /></div>
				<div class="login-box" style="margin-top: 20px;" >

					<h3 class="title">登录商城</h3>
					<h5 :style="colorStyle" id="tips">{{tips}}</h5>
					<div class="clear"></div>

					<div class="login-form"  style="background: none; margin-top: 15px;">
						<form>
							<div class="user-name"  style="margin-top: 20px;">
								<label for="user"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></label>
								<input type="text" v-model="username" placeholder="邮箱/手机/用户名" @blur="checkUserName">
							</div>
							<div class="user-pass"  style="margin-top: 20px;">
								<label for="password"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></label>
								<input type="password" v-model="password" placeholder="请输入密码" @blur="checkUserName">
							</div>
						</form>
					</div>

					<div class="login-links" >
						<label for="remember-me"><input id="remember-me" type="checkbox">记住密码</label>
						<a href="#" class="am-fr">忘记密码</a>
						<br />
					</div>
					<div class="am-cf">
						<input type="button" @click="doSubmit" value="登 录" class="am-btn am-btn-primary am-btn-sm">
					</div>
					<div class="am-cf">
						<input type="button" onclick="javascript:window.location.href='register.html'" value="注 册" class="am-btn am-btn-default am-btn-sm">
					</div>
					<div class="partner">
						
					</div>

				</div>
			</div>
		</div>

		<div class="footer ">
			<div class="footer-hd ">

			</div>
			<div class="footer-bd ">
				<p>
					<a href="# ">联系我们</a>
					<a href="# ">网站地图</a>
				</p>
			</div>
		</div>
		
		<script type="text/javascript" src="static/js/jquery-1.9.min.js" ></script>
		<script type="text/javascript" src="static/js/cookie_utils.js" ></script>
		<script type="text/javascript" src="static/js/vue.js" ></script>
		<script type="text/javascript" src="static/js/utils.js" ></script>
		<script type="text/javascript" src="static/js/axios.min.js" ></script>
		<script type="text/javascript" src="static/js/base.js" ></script>
		<script type="text/javascript">
			// axios.defaults.timeout = 2000;
			axios.defaults.baseURL = "http://127.0.0.1:9999";
			// axios.defaults.baseURL = "http://127.0.0.1:8080";
			/*$("#submitBtn").click(function(){
				//1.接收账号和密码
				var name = $("#username").val();
				var pwd = $("#password").val();
				//进行数据的合法性校验
				if (name.length<8 || name.length>20) {
					$("#tips").html("<label style='color:red'>账号长度必须为8-20个字符!</label> ");
				}else{
					if (name.length<8 || name.length>20) {
						$("#tips").html("<label style='color:red'>密码长度必须为8-20个字符!</label> ");
					}else{
						$.get("http://localhost:8080/user/login",{username:name,password:pwd},function(res){
							console.log(res);
							if (res.code == 10000){
								var userInfo = res.data;
								//登陆成功,我们需要将用户信息提交到到index
								//cookie cookie是浏览器端的缓存文件,容量大小受浏览器产品限制
								//localStorage/sessionStorage 为了存储更大容量的数据
								//1.cookie的作用
								// document.cookie = "username="+userInfo.username;
								// document.cookie = "userimg="+userInfo.userImg;
                                // setCookieValue("username",userInfo.username);
                                // setCookieValue("userimg",userInfo.Img);

                                localStorage.setItem("user",JSON.stringify(userInfo));


								 window.location.href="index.html";
							}else{
								//登陆失败
								$("#tips").html("<label style='color:red'>"+res.msg+"</label> ");
							}
						},"json");
					}
				}

			});*/
			let vm = new Vue({
				el:"#container",
				data:{
					username:"",
					password:"",
					tips:"",
					colorStyle:"color:red",
					isRight:false
				},
				methods:{
					doSubmit:function () {
						//校验
						vm.checkUserName();
						if (!vm.isRight){
							vm.tips = "用户名或密码格式不正确";
							return;
						}else{
							axios.get("/user/login", {
								params:{
									username: vm.username,
									password: vm.password
								}
							}).then(res => {
								let vo = res.data;
								console.log(vo);
								if (vo.code == 10000){
									//如果登陆成功,就把token存在cookie中
									setCookieValue("token",vo.token);
									setCookieValue("username",vo.data.username);
									setCookieValue("userId",vo.data.userId);
									setCookieValue("userImg",vo.data.userImg);

									window.location.href = "index.html";
								}else {
									vm.tips = vo.msg;
								}
							}).catch(res=>{
								alert(res);
							});
						}
					},
					//校验输入格式
					checkUserName:function () {
						vm.isRight=false;
						if (vm.username==""){
							vm.tips = "用户名不能为空";
						}else if (vm.username.length<8 || vm.username.length>20){
							vm.tips = "账号长度必须为8-20个字符";
						}else{
							if (vm.password == ""){
								vm.tips="请输入密码!";
							}else if(vm.password.length<6 || vm.password.length>16){
								vm.tips="密码长度必须为6-16位字符";
							}else{
								vm.tips="";
								vm.isRight=true;
							}
						}
					}
				}
			});
		</script>
	</body>

</html>